<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>第三节：URL与视图 | Flask进阶课程课件</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html" />
    
    
    <link rel="prev" href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.3"
        data-chapter-title="第三节：URL与视图"
        data-filepath="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-san-jie-ff1a-url-yu-shi-tu-han-shu.md"
        data-basepath=".."
        data-revision="Tue Aug 14 2018 17:50:50 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1.html">
            
                
                    <a href="../chapter1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        第一章：学前准备
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/di-yi-jie-ff1a-xu-ni-huan-jing.html">
            
                
                    <a href="../chapter1/di-yi-jie-ff1a-xu-ni-huan-jing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        第一节：虚拟环境
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="di-er-zhang-ff1a-ren-shi-web.html">
            
                
                    <a href="../di-er-zhang-ff1a-ren-shi-web.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        第二章：认识web
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        第三章：URL与视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-yi-jie-ff1a-di-yi-ge-flask-cheng-xu.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-yi-jie-ff1a-di-yi-ge-flask-cheng-xu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        第一节：Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        第二节：项目配置
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.3" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-san-jie-ff1a-url-yu-shi-tu-han-shu.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-san-jie-ff1a-url-yu-shi-tu-han-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        第三节：URL与视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html">
            
                
                    <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        第四节：关于响应
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        第四章：模版
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-yi-jie-ff1a-mo-ban-jian-jie.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-yi-jie-ff1a-mo-ban-jian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        第一节：模版简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-er-jie-ff1a-jinja2-mo-ban-gai-shu.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-er-jie-ff1a-jinja2-mo-ban-gai-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        第二节：Jinja2模版概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-san-jie-ff1a-jinja2-guo-lv-qi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-san-jie-ff1a-jinja2-guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        第三节：过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-si-jie-ff1a-jinja2-mo-ban-kong-zhi-yu-ju.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-si-jie-ff1a-jinja2-mo-ban-kong-zhi-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        第四节：控制语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-wu-jie-ff1a-jinja2-mo-ban-ce-shi-qi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-wu-jie-ff1a-jinja2-mo-ban-ce-shi-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        第五节：测试器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-liu-jie-ff1a-hong-he-import-yu-ju.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-liu-jie-ff1a-hong-he-import-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        第六节：宏和import语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-qijie-ff1a-include-he-set-yu-ju.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-qijie-ff1a-include-he-set-yu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        第七节：include和set语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-ba-jie-ff1a-mo-ban-ji-cheng.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-ba-jie-ff1a-mo-ban-ji-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        第八节：模版继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-jiu-jie-ff1a-zhuan-yi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-jiu-jie-ff1a-zhuan-yi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        第九节：转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-jie-ff1a-shu-ju-lei-xing-he-yun-suan-fu.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-jie-ff1a-shu-ju-lei-xing-he-yun-suan-fu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.10.</b>
                        
                        第十节：数据类型和运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-yi-jie-ff1a-jing-tai-wen-jian-de-pei-zhi.html">
            
                
                    <a href="../di-si-zhang-ff1a-flask-ru-men-ff08-mo-ban-ff09/di-shi-yi-jie-ff1a-jing-tai-wen-jian-de-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.11.</b>
                        
                        第十一节：静态文件的配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji.html">
            
                
                    <a href="../di-wu-zhang-ff1a-shi-tu-gao-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        第五章：视图高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji/di-yi-jie-ff1a-lei-shi-tu.html">
            
                
                    <a href="../di-wu-zhang-ff1a-shi-tu-gao-ji/di-yi-jie-ff1a-lei-shi-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        第一节：类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="di-wu-zhang-ff1a-shi-tu-gao-ji/di-er-jie-ff1a-lan-tu.html">
            
                
                    <a href="../di-wu-zhang-ff1a-shi-tu-gao-ji/di-er-jie-ff1a-lan-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        第二节：蓝图和子域名
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第六章：SQLAlchemy数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-mysql-shu-ju-ku-de-an-zhuang.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        第一节：MySQL数据库的安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-sqlalchemy-jie-shao-he-ji-ben-shi-yong.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-yi-jie-ff1a-sqlalchemy-jie-shao-he-ji-ben-shi-yong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        第二节：SQLAlchemy介绍和基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-san-jie-ff1a-sqlalchemy-de-orm.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        第三节：SQLAlchemy的ORM（1）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1a-sqlalchemy-de-orm-2.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1a-sqlalchemy-de-orm-2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        第四节：SQLAlchemy的ORM（2）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-wu-jie-ff1a-sqlalchemy-de-orm-3.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-wu-jie-ff1a-sqlalchemy-de-orm-3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        第五节：SQLAlchemy的ORM（3）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-liu-jie-ff1a-sqlalchemy-de-orm-4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        第六节：SQLAlchemy的ORM（4）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-qi-jie-ff1a-sqlalchemy-de-orm-5.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-qi-jie-ff1a-sqlalchemy-de-orm-5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        第七节：SQLAlchemy的ORM（5）
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html">
            
                
                    <a href="../di-liu-zhang-ff1a-sqlalchemy-shu-ju-ku/di-si-jie-ff1aflask-sqlalchemy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        第八节：Flask-SQLAlchemy
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="di-qi-zhang-ff1a-flask-script.html">
            
                
                    <a href="../di-qi-zhang-ff1a-flask-script.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        第七章：Flask-Script
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="di-bazhang-ff1a-alembic-jiao-cheng.html">
            
                
                    <a href="../di-bazhang-ff1a-alembic-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        第八章：alembic教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="di-ba-zhang-ff1a-flask-migrate.html">
            
                
                    <a href="../di-ba-zhang-ff1a-flask-migrate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        第九章：Flask-Migrate
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10" data-path="di-jiu-zhang-ff1a-flask-wtf.html">
            
                
                    <a href="../di-jiu-zhang-ff1a-flask-wtf.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        第十章：Flask-WTF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11" data-path="di-shi-yi-zhang-ff1a-cookie-he-session.html">
            
                
                    <a href="../di-shi-yi-zhang-ff1a-cookie-he-session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        第十一章：cookie和session
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="di-shi-zhang-ff1a-shang-xia-wen.html">
            
                
                    <a href="../di-shi-zhang-ff1a-shang-xia-wen.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        第十二章：Flask上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="13" data-path="di-shi-yi-zhang-ff1a-flask-xin-hao-ji-zhi.html">
            
                
                    <a href="../di-shi-yi-zhang-ff1a-flask-xin-hao-ji-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                        第十三章：Flask信号机制
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14" data-path="di-shi-si-zhang-ff1a-flask.html">
            
                
                    <a href="../di-shi-si-zhang-ff1a-flask.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                        第十四章：Flask-Restful
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="14.1" data-path="di-shi-si-zhang-ff1a-flask/di-yi-jie-ff1a-restful-api-gui-fan.html">
            
                
                    <a href="../di-shi-si-zhang-ff1a-flask/di-yi-jie-ff1a-restful-api-gui-fan.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.1.</b>
                        
                        第一节：Restful API规范
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14.2" data-path="di-shi-si-zhang-ff1a-flask/di-er-jie-ff1a-flask-restful-cha-jian.html">
            
                
                    <a href="../di-shi-si-zhang-ff1a-flask/di-er-jie-ff1a-flask-restful-cha-jian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.2.</b>
                        
                        第二节：Flask-Restful插件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="15" data-path="di-shi-er-zhang-ff1a-memcached-jiao-cheng.html">
            
                
                    <a href="../di-shi-er-zhang-ff1a-memcached-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>15.</b>
                        
                        第十五章：memcached教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="16" data-path="di-shi-sizhang-ff1a-redis-jiao-cheng.html">
            
                
                    <a href="../di-shi-sizhang-ff1a-redis-jiao-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>16.</b>
                        
                        第十六章：redis教程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="17" data-path="di-shi-liu-zhang-ff1a-csrf-gong-ji.html">
            
                
                    <a href="../di-shi-liu-zhang-ff1a-csrf-gong-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>17.</b>
                        
                        第十七章：CSRF攻击
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="18" data-path="di-shi-ba-zhang-ff1a-qi-niu-yun-pei-zhi.html">
            
                
                    <a href="../di-shi-ba-zhang-ff1a-qi-niu-yun-pei-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>18.</b>
                        
                        第十八章：七牛云配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="19" data-path="di-shi-jiu-zhang-ff1a-bu-shu-flask-xiang-mu.html">
            
                
                    <a href="../di-shi-jiu-zhang-ff1a-bu-shu-flask-xiang-mu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>19.</b>
                        
                        第十九章：部署Flask项目
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Flask进阶课程课件</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="url&#x4E0E;&#x89C6;&#x56FE;">URL&#x4E0E;&#x89C6;&#x56FE;</h1>
<h3 id="url&#x4E0E;&#x51FD;&#x6570;&#x7684;&#x6620;&#x5C04;&#xFF1A;">URL&#x4E0E;&#x51FD;&#x6570;&#x7684;&#x6620;&#x5C04;&#xFF1A;</h3>
<p>&#x4ECE;&#x4E4B;&#x524D;&#x7684;<code>helloworld.py</code>&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x770B;&#x5230;&#xFF0C;&#x4E00;&#x4E2A;<code>URL</code>&#x8981;&#x4E0E;&#x6267;&#x884C;&#x51FD;&#x6570;&#x8FDB;&#x884C;&#x6620;&#x5C04;&#xFF0C;&#x4F7F;&#x7528;&#x7684;&#x662F;<code>@app.route</code>&#x88C5;&#x9970;&#x5668;&#x3002;<code>@app.route</code>&#x88C5;&#x9970;&#x5668;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;<code>URL</code>&#x7684;&#x89C4;&#x5219;&#x6765;&#x8FDB;&#x884C;&#x66F4;&#x52A0;&#x8BE6;&#x7EC6;&#x7684;&#x6620;&#x5C04;&#xFF0C;&#x6BD4;&#x5982;&#x73B0;&#x5728;&#x8981;&#x6620;&#x5C04;&#x4E00;&#x4E2A;&#x6587;&#x7AE0;&#x8BE6;&#x60C5;&#x7684;<code>URL</code>&#xFF0C;&#x6587;&#x7AE0;&#x8BE6;&#x60C5;&#x7684;<code>URL</code>&#x662F;<code>/article/id/</code>&#xFF0C;id&#x6709;&#x53EF;&#x80FD;&#x4E3A;1&#x3001;2&#x3001;3...,&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x65B9;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator">   @app.route(&apos;/article/&lt;id&gt;/&apos;)</span>
   <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">article</span><span class="hljs-params">(id)</span>:</span>
       <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;%s article detail&apos;</span> % id
</code></pre>
<p>&#x5176;&#x4E2D;<code>&lt;id&gt;</code>&#xFF0C;&#x5C16;&#x62EC;&#x53F7;&#x662F;&#x56FA;&#x5B9A;&#x5199;&#x6CD5;&#xFF0C;&#x8BED;&#x6CD5;&#x4E3A;<code>&lt;variable&gt;</code>&#xFF0C;<code>variable</code>&#x9ED8;&#x8BA4;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x5982;&#x679C;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x8981;&#x5199;&#x6210;<code>&lt;converter:variable&gt;</code>&#xFF0C;&#x5176;&#x4E2D;<code>converter</code>&#x5C31;&#x662F;&#x7C7B;&#x578B;&#x540D;&#x79F0;&#xFF0C;&#x53EF;&#x4EE5;&#x6709;&#x4EE5;&#x4E0B;&#x51E0;&#x79CD;&#xFF1A;</p>
<ul>
<li>string: &#x9ED8;&#x8BA4;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x63A5;&#x53D7;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x659C;&#x6760;<code>/</code>&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</li>
<li>int: &#x6574;&#x5F62;</li>
<li>float: &#x6D6E;&#x70B9;&#x578B;&#x3002;</li>
<li>path&#xFF1A; &#x548C;<code>string</code>&#x7C7B;&#x4F3C;&#xFF0C;&#x4F46;&#x662F;&#x53EF;&#x4EE5;&#x4F20;&#x9012;&#x659C;&#x6760;<code>/</code>&#x3002;</li>
<li>uuid&#xFF1A; <code>uuid</code>&#x7C7B;&#x578B;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</li>
<li><p>any&#xFF1A;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x591A;&#x79CD;&#x8DEF;&#x5F84;&#xFF0C;&#x8FD9;&#x4E2A;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x6765;&#x8FDB;&#x884C;&#x8BF4;&#x660E;:</p>
<pre><code class="lang-python"><span class="hljs-decorator">  @app.route(&apos;/&lt;any(article,blog):url_path&gt;/&apos;)</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">item</span><span class="hljs-params">(url_path)</span>:</span>
      <span class="hljs-keyword">return</span> url_path
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;<code>item</code>&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x4E24;&#x4E2A;<code>URL</code>,&#x4E00;&#x4E2A;&#x662F;<code>/article/</code>&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x662F;<code>/blog/</code>&#x3002;&#x5E76;&#x4E14;&#xFF0C;&#x4E00;&#x5B9A;&#x8981;&#x4F20;<code>url_path</code>&#x53C2;&#x6570;&#xFF0C;&#x5F53;&#x7136;&#x8FD9;&#x4E2A;<code>url_path</code>&#x7684;&#x540D;&#x79F0;&#x53EF;&#x4EE5;&#x968F;&#x4FBF;&#x3002;</p>
</li>
</ul>
<p>&#x5982;&#x679C;&#x4E0D;&#x60F3;&#x5B9A;&#x5236;&#x5B50;&#x8DEF;&#x5F84;&#x6765;&#x4F20;&#x9012;&#x53C2;&#x6570;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F20;&#x7EDF;&#x7684;<code>?=</code>&#x7684;&#x5F62;&#x5F0F;&#x6765;&#x4F20;&#x9012;&#x53C2;&#x6570;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;<code>/article?id=xxx</code>&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>request.args.get(&apos;id&apos;)</code>&#x6765;&#x83B7;&#x53D6;<code>id</code>&#x7684;&#x503C;&#x3002;&#x5982;&#x679C;&#x662F;<code>post</code>&#x65B9;&#x6CD5;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>request.form.get(&apos;id&apos;)</code>&#x6765;&#x8FDB;&#x884C;&#x83B7;&#x53D6;&#x3002;</p>
<h3 id="&#x6784;&#x9020;url&#xFF08;urlfor&#xFF09;&#xFF1A;">&#x6784;&#x9020;URL&#xFF08;url_for&#xFF09;&#xFF1A;</h3>
<p>&#x4E00;&#x822C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;<code>URL</code>&#x5C31;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x5230;&#x67D0;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x5982;&#x679C;&#x53CD;&#x8FC7;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x600E;&#x4E48;&#x53BB;&#x83B7;&#x5F97;&#x8FD9;&#x4E2A;<code>URL</code>&#x5462;&#xFF1F;<code>url_for</code>&#x51FD;&#x6570;&#x5C31;&#x53EF;&#x4EE5;&#x5E2E;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x3002;<code>url_for()</code>&#x51FD;&#x6570;&#x63A5;&#x6536;&#x4E24;&#x4E2A;&#x53CA;&#x4EE5;&#x4E0A;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x4ED6;&#x63A5;&#x6536;<strong>&#x51FD;&#x6570;&#x540D;</strong>&#x4F5C;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x63A5;&#x6536;&#x5BF9;&#x5E94;<strong>URL&#x89C4;&#x5219;&#x7684;&#x547D;&#x540D;&#x53C2;&#x6570;</strong>&#xFF0C;&#x5982;&#x679C;&#x8FD8;&#x51FA;&#x73B0;&#x5176;&#x4ED6;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x4F1A;&#x6DFB;&#x52A0;&#x5230;<code>URL</code>&#x7684;&#x540E;&#x9762;&#x4F5C;&#x4E3A;<strong>&#x67E5;&#x8BE2;&#x53C2;&#x6570;</strong>&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x6784;&#x5EFA;<code>URL</code>&#x7684;&#x65B9;&#x5F0F;&#x800C;&#x9009;&#x62E9;&#x76F4;&#x63A5;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x62FC;<code>URL</code>&#x7684;&#x539F;&#x56E0;&#x6709;&#x4E24;&#x70B9;&#xFF1A;</p>
<ol>
<li>&#x5C06;&#x6765;&#x5982;&#x679C;&#x4FEE;&#x6539;&#x4E86;<code>URL</code>&#xFF0C;&#x4F46;&#x6CA1;&#x6709;&#x4FEE;&#x6539;&#x8BE5;<code>URL</code>&#x5BF9;&#x5E94;&#x7684;&#x51FD;&#x6570;&#x540D;&#xFF0C;&#x5C31;&#x4E0D;&#x7528;&#x5230;&#x5904;&#x53BB;&#x66FF;&#x6362;<code>URL</code>&#x4E86;&#x3002;</li>
<li><code>url_for()</code>&#x51FD;&#x6570;&#x4F1A;&#x8F6C;&#x4E49;&#x4E00;&#x4E9B;&#x7279;&#x6B8A;&#x5B57;&#x7B26;&#x548C;<code>unicode</code>&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8FD9;&#x4E9B;&#x4E8B;&#x60C5;<code>url_for</code>&#x4F1A;&#x81EA;&#x52A8;&#x7684;&#x5E2E;&#x6211;&#x4EEC;&#x641E;&#x5B9A;&#x3002;</li>
</ol>
<p>&#x4E0B;&#x9762;&#x7528;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x6765;&#x8FDB;&#x884C;&#x89E3;&#x91CA;&#xFF1A;</p>
<pre><code class="lang-python">    <span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask,url_for
    app = Flask(__name__)

<span class="hljs-decorator">    @app.route(&apos;/article/&lt;id&gt;/&apos;)</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">article</span><span class="hljs-params">(id)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;%s article detail&apos;</span> % id

<span class="hljs-decorator">    @app.route(&apos;/&apos;)</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span>
        print(url_for(<span class="hljs-string">&quot;article&quot;</span>,id=<span class="hljs-number">1</span>))
        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;&#x9996;&#x9875;&quot;</span>
</code></pre>
<pre><code>&#x5728;&#x8BBF;&#x95EE;index&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x6253;&#x5370;&#x51FA;`/article/1/`&#x3002;
</code></pre><h3 id="&#x81EA;&#x5B9A;&#x4E49;url&#x8F6C;&#x6362;&#x5668;&#xFF1A;">&#x81EA;&#x5B9A;&#x4E49;URL&#x8F6C;&#x6362;&#x5668;&#xFF1A;</h3>
<p>&#x521A;&#x521A;&#x5728;URL&#x6620;&#x5C04;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x770B;&#x5230;&#x4E86;Flask&#x5185;&#x7F6E;&#x4E86;&#x51E0;&#x79CD;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x7684;&#x8F6C;&#x6362;&#x5668;&#xFF0C;&#x6BD4;&#x5982;&#x6709;int/string&#x7B49;&#x3002;&#x5982;&#x679C;Flask&#x5185;&#x7F6E;&#x7684;&#x8F6C;&#x6362;&#x5668;&#x4E0D;&#x80FD;&#x6EE1;&#x8DB3;&#x4F60;&#x7684;&#x9700;&#x6C42;&#xFF0C;&#x6B64;&#x65F6;&#x4F60;&#x53EF;&#x4EE5;&#x81EA;&#x5B9A;&#x4E49;&#x8F6C;&#x6362;&#x5668;&#x3002;&#x81EA;&#x5B9A;&#x4E49;&#x8F6C;&#x6362;&#x5668;&#xFF0C;&#x9700;&#x8981;&#x6EE1;&#x8DB3;&#x4EE5;&#x4E0B;&#x51E0;&#x4E2A;&#x6761;&#x4EF6;&#xFF1A;</p>
<ol>
<li><p>&#x8F6C;&#x6362;&#x5668;&#x662F;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;&#x4E14;&#x5FC5;&#x987B;&#x7EE7;&#x627F;&#x81EA;werkzeug.routing.BaseConverter&#x3002;</p>
</li>
<li><p>&#x5728;&#x8F6C;&#x6362;&#x5668;&#x7C7B;&#x4E2D;&#xFF0C;&#x5B9E;&#x73B0;to_python(self,value)&#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x5C06;&#x4F1A;&#x4F20;&#x9012;&#x5230;view&#x51FD;&#x6570;&#x4E2D;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x3002;</p>
</li>
<li><p>&#x5728;&#x8F6C;&#x6362;&#x5668;&#x7C7B;&#x4E2D;&#xFF0C;&#x5B9E;&#x73B0;to_url(self,values)&#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x5C06;&#x4F1A;&#x5728;&#x8C03;&#x7528;url_for&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#x751F;&#x6210;&#x7B26;&#x5408;&#x8981;&#x6C42;&#x7684;URL&#x5F62;&#x5F0F;&#x3002;</p>
</li>
</ol>
<p>&#x6BD4;&#x5982;&#xFF0C;&#x62FF;&#x4E00;&#x4E2A;&#x5B98;&#x65B9;&#x7684;&#x4F8B;&#x5B50;&#x6765;&#x8BF4;&#xFF0C;Reddit&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5728;URL&#x4E2D;&#x7528;&#x4E00;&#x4E2A;&#x52A0;&#x53F7;&#xFF08;+&#xFF09;&#x9694;&#x5F00;&#x793E;&#x533A;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x65B9;&#x4FBF;&#x540C;&#x65F6;&#x67E5;&#x770B;&#x6765;&#x81EA;&#x591A;&#x4E2A;&#x793E;&#x533A;&#x7684;&#x5E16;&#x5B50;&#x3002;&#x6BD4;&#x5982;&#x8BBF;&#x95EE;&#x201C;www.reddit.com\/r\/flask+lisp\/&#x201D;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x540C;&#x65F6;&#x53EF;&#x4EE5;&#x67E5;&#x770B;flask&#x548C;lisp&#x4E24;&#x4E2A;&#x793E;&#x533A;&#x7684;&#x5E16;&#x5B50;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x81EA;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x8F6C;&#x6362;&#x5668;&#x6765;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment">#coding: utf-8</span>
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask,url_for
<span class="hljs-keyword">from</span> werkzeug.routing <span class="hljs-keyword">import</span> BaseConverter

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ListConverter</span><span class="hljs-params">(BaseConverter)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,url_map,separator=<span class="hljs-string">&apos;+&apos;</span>)</span>:</span>
        super(ListConverter,self).__init__(url_map)
        self.separator = separator

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">to_python</span><span class="hljs-params">(self, value)</span>:</span>
        <span class="hljs-keyword">return</span> value.split(self.separator)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">to_url</span><span class="hljs-params">(self, values)</span>:</span>
        <span class="hljs-keyword">return</span> self.separator.join(BaseConverter.to_url(self,value) <span class="hljs-keyword">for</span> value <span class="hljs-keyword">in</span> values)  

app.url_map.converters[<span class="hljs-string">&apos;list&apos;</span>] = ListConverter

<span class="hljs-decorator">@app.route(&apos;/community1/&lt;list:page_names&gt;&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">community1</span><span class="hljs-params">(page_names)</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;%s+%s&apos;</span> % tuple(page_names)

<span class="hljs-decorator">@app.route(&apos;/community2/&lt;list(&apos;|&apos;):page_names&gt;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">community2</span><span class="hljs-params">(page_names)</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;%s|%s&quot;</span> % tuple(page_names)
</code></pre>
<p>communityu1&#x4F7F;&#x7528;&#x7684;&#x662F;&#x9ED8;&#x8BA4;&#x7684;+&#x53F7;&#x8FDB;&#x884C;&#x8FDE;&#x63A5;&#xFF0C;&#x800C;&#x7B2C;&#x4E8C;&#x79CD;&#x65B9;&#x5F0F;&#x4F7F;&#x7528;&#x4E86;|&#x8FDB;&#x884C;&#x8FDE;&#x63A5;&#x3002;</p>
<h3 id="&#x6307;&#x5B9A;url&#x672B;&#x5C3E;&#x7684;&#x659C;&#x6760;&#xFF1A;">&#x6307;&#x5B9A;URL&#x672B;&#x5C3E;&#x7684;&#x659C;&#x6760;&#xFF1A;</h3>
<p>&#x6709;&#x4E9B;<code>URL</code>&#x7684;&#x672B;&#x5C3E;&#x662F;&#x6709;&#x659C;&#x6760;&#x7684;&#xFF0C;&#x6709;&#x4E9B;<code>URL</code>&#x672B;&#x5C3E;&#x662F;&#x6CA1;&#x6709;&#x659C;&#x6760;&#x7684;&#x3002;&#x8FD9;&#x5176;&#x5B9E;&#x662F;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;<code>URL</code>&#x3002;</p>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator"> @app.route(&apos;/article/&apos;)</span>
 <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">articles</span><span class="hljs-params">()</span>:</span>
     <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;&#x6587;&#x7AE0;&#x5217;&#x8868;&#x9875;&apos;</span>
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x5F53;&#x8BBF;&#x95EE;&#x4E00;&#x4E2A;&#x7ED3;&#x5C3E;&#x4E0D;&#x5E26;&#x659C;&#x7EBF;&#x7684;<code>URL</code>&#xFF1A;<code>/article</code>&#xFF0C;&#x4F1A;&#x88AB;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x5E26;&#x659C;&#x7EBF;&#x7684;<code>URL</code>&#xFF1A;<code>/article/</code>&#x4E0A;&#x53BB;&#x3002;&#x4F46;&#x662F;&#x5F53;&#x6211;&#x4EEC;&#x5728;&#x5B9A;&#x4E49;<code>article</code>&#x7684;<code>url</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x672B;&#x5C3E;&#x6CA1;&#x6709;&#x52A0;&#x4E0A;&#x659C;&#x6760;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x8BBF;&#x95EE;&#x7684;&#x65F6;&#x5019;&#x53C8;&#x52A0;&#x4E0A;&#x4E86;&#x659C;&#x6760;&#xFF0C;&#x8FD9;&#x65F6;&#x5019;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x4E00;&#x4E2A;<code>404</code>&#x9519;&#x8BEF;&#x9875;&#x9762;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&quot;/article&quot;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">articles</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;&#x6587;&#x7AE0;&#x5217;&#x8868;&#x9875;&#x9762;&quot;</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x6CA1;&#x6709;&#x5728;&#x672B;&#x5C3E;&#x52A0;&#x659C;&#x6760;&#xFF0C;&#x56E0;&#x6B64;&#x5728;&#x8BBF;&#x95EE;<code>/article/</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x4E00;&#x4E2A;<code>404</code>&#x9519;&#x8BEF;&#x3002;</p>
<h3 id="&#x6307;&#x5B9A;http&#x65B9;&#x6CD5;&#xFF1A;">&#x6307;&#x5B9A;HTTP&#x65B9;&#x6CD5;&#xFF1A;</h3>
<p>&#x5728;<code>@app.route()</code>&#x4E2D;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;<code>methods</code>&#x6765;&#x6307;&#x5B9A;&#x672C;&#x65B9;&#x6CD5;&#x652F;&#x6301;&#x7684;<code>HTTP</code>&#x65B9;&#x6CD5;&#xFF0C;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EA;&#x80FD;&#x4F7F;&#x7528;<code>GET</code>&#x8BF7;&#x6C42;&#xFF0C;&#x770B;&#x4EE5;&#x4E0B;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-decorator">@app.route(&apos;/login/&apos;,methods=[&apos;GET&apos;,&apos;POST&apos;])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">login</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;login&apos;</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x88C5;&#x9970;&#x5668;&#x5C06;&#x8BA9;<code>login</code>&#x7684;<code>URL</code>&#x65E2;&#x80FD;&#x652F;&#x6301;<code>GET</code>&#x53C8;&#x80FD;&#x652F;&#x6301;<code>POST</code>&#x3002;</p>
<h3 id="&#x9875;&#x9762;&#x8DF3;&#x8F6C;&#x548C;&#x91CD;&#x5B9A;&#x5411;&#xFF1A;">&#x9875;&#x9762;&#x8DF3;&#x8F6C;&#x548C;&#x91CD;&#x5B9A;&#x5411;&#xFF1A;</h3>
<p>&#x91CD;&#x5B9A;&#x5411;&#x5206;&#x4E3A;&#x6C38;&#x4E45;&#x6027;&#x91CD;&#x5B9A;&#x5411;&#x548C;&#x6682;&#x65F6;&#x6027;&#x91CD;&#x5B9A;&#x5411;&#xFF0C;&#x5728;&#x9875;&#x9762;&#x4E0A;&#x4F53;&#x73B0;&#x7684;&#x64CD;&#x4F5C;&#x5C31;&#x662F;&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x4ECE;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x81EA;&#x52A8;&#x8DF3;&#x8F6C;&#x5230;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x3002;&#x6BD4;&#x5982;&#x7528;&#x6237;&#x8BBF;&#x95EE;&#x4E86;&#x4E00;&#x4E2A;&#x9700;&#x8981;&#x6743;&#x9650;&#x7684;&#x9875;&#x9762;&#xFF0C;&#x4F46;&#x662F;&#x8BE5;&#x7528;&#x6237;&#x5F53;&#x524D;&#x5E76;&#x6CA1;&#x6709;&#x767B;&#x5F55;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x5E94;&#x8BE5;&#x7ED9;&#x4ED6;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x767B;&#x5F55;&#x9875;&#x9762;&#x3002;</p>
<ul>
<li><p>&#x6C38;&#x4E45;&#x6027;&#x91CD;&#x5B9A;&#x5411;&#xFF1A;<code>http</code>&#x7684;&#x72B6;&#x6001;&#x7801;&#x662F;<code>301</code>&#xFF0C;&#x591A;&#x7528;&#x4E8E;&#x65E7;&#x7F51;&#x5740;&#x88AB;&#x5E9F;&#x5F03;&#x4E86;&#x8981;&#x8F6C;&#x5230;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7F51;&#x5740;&#x786E;&#x4FDD;&#x7528;&#x6237;&#x7684;&#x8BBF;&#x95EE;&#xFF0C;&#x6700;&#x7ECF;&#x5178;&#x7684;&#x5C31;&#x662F;&#x4EAC;&#x4E1C;&#x7F51;&#x7AD9;&#xFF0C;&#x4F60;&#x8F93;&#x5165;<code>www.jingdong.com</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x88AB;&#x91CD;&#x5B9A;&#x5411;&#x5230;<code>www.jd.com</code>&#xFF0C;&#x56E0;&#x4E3A;<code>jingdong.com</code>&#x8FD9;&#x4E2A;&#x7F51;&#x5740;&#x5DF2;&#x7ECF;&#x88AB;&#x5E9F;&#x5F03;&#x4E86;&#xFF0C;&#x88AB;&#x6539;&#x6210;<code>jd.com</code>&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x5E94;&#x8BE5;&#x7528;&#x6C38;&#x4E45;&#x91CD;&#x5B9A;&#x5411;&#x3002;</p>
</li>
<li><p>&#x6682;&#x65F6;&#x6027;&#x91CD;&#x5B9A;&#x5411;&#xFF1A;<code>http</code>&#x7684;&#x72B6;&#x6001;&#x7801;&#x662F;<code>302</code>&#xFF0C;&#x8868;&#x793A;&#x9875;&#x9762;&#x7684;&#x6682;&#x65F6;&#x6027;&#x8DF3;&#x8F6C;&#x3002;&#x6BD4;&#x5982;&#x8BBF;&#x95EE;&#x4E00;&#x4E2A;&#x9700;&#x8981;&#x6743;&#x9650;&#x7684;&#x7F51;&#x5740;&#xFF0C;&#x5982;&#x679C;&#x5F53;&#x524D;&#x7528;&#x6237;&#x6CA1;&#x6709;&#x767B;&#x5F55;&#xFF0C;&#x5E94;&#x8BE5;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x767B;&#x5F55;&#x9875;&#x9762;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5E94;&#x8BE5;&#x7528;&#x6682;&#x65F6;&#x6027;&#x91CD;&#x5B9A;&#x5411;&#x3002;</p>
</li>
</ul>
<p>&#x5728;<code>flask</code>&#x4E2D;&#xFF0C;&#x91CD;&#x5B9A;&#x5411;&#x662F;&#x901A;&#x8FC7;<code>flask.redirect(location,code=302)</code>&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;<code>location</code>&#x8868;&#x793A;&#x9700;&#x8981;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x7684;<code>URL</code>&#xFF0C;&#x5E94;&#x8BE5;&#x914D;&#x5408;&#x4E4B;&#x524D;&#x8BB2;&#x7684;<code>url_for()</code>&#x51FD;&#x6570;&#x6765;&#x4F7F;&#x7528;&#xFF0C;<code>code</code>&#x8868;&#x793A;&#x91C7;&#x7528;&#x54EA;&#x4E2A;&#x91CD;&#x5B9A;&#x5411;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;<code>302</code>&#x4E5F;&#x5373;<code>&#x6682;&#x65F6;&#x6027;&#x91CD;&#x5B9A;&#x5411;</code>&#xFF0C;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x6210;<code>301</code>&#x6765;&#x5B9E;&#x73B0;&#x6C38;&#x4E45;&#x6027;&#x91CD;&#x5B9A;&#x5411;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x5173;&#x4E8E;&#x5728;<code>flask</code>&#x4E2D;&#x600E;&#x4E48;&#x4F7F;&#x7528;&#x91CD;&#x5B9A;&#x5411;&#xFF1A;</p>
<pre><code class="lang-python"> <span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask,url_for,redirect

 app = Flask(__name__)
 app.debug = <span class="hljs-keyword">True</span>

<span class="hljs-decorator"> @app.route(&apos;/login/&apos;,methods=[&apos;GET&apos;,&apos;POST&apos;])</span>
 <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">login</span><span class="hljs-params">()</span>:</span>
     <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;login page&apos;</span>

<span class="hljs-decorator"> @app.route(&apos;/profile/&apos;,methods=[&apos;GET&apos;,&apos;POST&apos;])</span>
 <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">profile</span><span class="hljs-params">()</span>:</span>
     name = request.args.get(<span class="hljs-string">&apos;name&apos;</span>)

     <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> name:
     <span class="hljs-comment"># &#x5982;&#x679C;&#x6CA1;&#x6709;name&#xFF0C;&#x8BF4;&#x660E;&#x6CA1;&#x6709;&#x767B;&#x5F55;&#xFF0C;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x767B;&#x5F55;&#x9875;&#x9762;</span>
         <span class="hljs-keyword">return</span> redirect(url_for(<span class="hljs-string">&apos;login&apos;</span>))
     <span class="hljs-keyword">else</span>:
         <span class="hljs-keyword">return</span> name
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-er-jie-ff1a-xiang-mu-pei-zhi.html" class="navigation navigation-prev " aria-label="Previous page: 第二节：项目配置"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../di-san-zhang-ff1a-flask-ru-men-2014-2014-url/di-si-jie-ff1a-guan-yu-xiang-ying.html" class="navigation navigation-next " aria-label="Next page: 第四节：关于响应"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
